﻿using System;
using System.Drawing;
using System.Windows.Forms;
using ImageFan.Controls;
using ImageFan.ImageHandling;

namespace ImageFan.Forms
{
    internal partial class FullScreenImage
        : Form
    {
        public FullScreenImage(ImageForm parentForm, ThumbnailsSequence thumbnailsSequence,
                               ImageFile imageFile)
        {
            InitializeComponent();

            foreach (Control aControl in Controls)
            {
                aControl.Cursor = Cursor;
                aControl.Click += new EventHandler(FullScreenImage_Click);
            }

            _parentForm = parentForm;
            _thumbnailsSequence = thumbnailsSequence;

            ImageSetUp(imageFile);
        }
        

        #region Private

        private ImageForm _parentForm;
        private ThumbnailsSequence _thumbnailsSequence;
        private ImageFile _imageFile;
        private Image _fullScreenImage;
        
        private void ImageSetUp(ImageFile imageFile)
        {
            var previousImageFile = _imageFile;

            _imageFile = imageFile;

            _fullScreenImage = _imageFile.FullScreenImage;

            Width = GlobalData.ScreenWidth;
            Height = GlobalData.ScreenHeight;

            if ((GlobalData.ScreenWidth >= _fullScreenImage.Width) &&
                (GlobalData.ScreenHeight >= _fullScreenImage.Height))
                pbFullScreenImage.Image = _fullScreenImage;
            else
                pbFullScreenImage.Image = ImageResizer.CreateResizedFullScreenImage(_fullScreenImage);

            if (previousImageFile != null && previousImageFile != _imageFile)
            {
                previousImageFile.DisposeFullScreenImage();
                previousImageFile.DisposeImageFromDisc();
            }
        }

        private void FullScreenImage_KeyDown(object sender, KeyEventArgs args)
        {
            if ((args.KeyCode == Keys.Escape) || (args.KeyCode == Keys.Enter))
                Close();

            else if ((args.KeyCode == Keys.Space) || (args.KeyCode == Keys.Down) || (args.KeyCode == Keys.Right) ||
                     (args.KeyCode == Keys.PageDown) || (args.KeyCode == Keys.S) || (args.KeyCode == Keys.D))
            {
                if (_thumbnailsSequence.NextThumbnail != null)
                {
                    _thumbnailsSequence.MoveToNextThumbnail();
                    ImageSetUp(_thumbnailsSequence.CurrentThumbnail.ImageFile);
                }
            }

            else if ((args.KeyCode == Keys.Back) || (args.KeyCode == Keys.Up) || (args.KeyCode == Keys.Left) ||
                     (args.KeyCode == Keys.PageUp) || (args.KeyCode == Keys.W) || (args.KeyCode == Keys.A))
            {
                if (_thumbnailsSequence.PreviousThumbnail != null)
                {
                    _thumbnailsSequence.MoveToPreviousThumbnail();
                    ImageSetUp(_thumbnailsSequence.CurrentThumbnail.ImageFile);
                }
            }
        }

        private void FullScreenImage_Click(object sender, EventArgs args)
        {
            Close();
        }

        private void FullScreenImage_MouseWheel(object sender, MouseEventArgs args)
        {
            if (args.Delta < 0)
            {
                if (_thumbnailsSequence.NextThumbnail != null)
                {
                    _thumbnailsSequence.MoveToNextThumbnail();
                    ImageSetUp(_thumbnailsSequence.CurrentThumbnail.ImageFile);
                }
            }

            else if (args.Delta > 0)
            {
                if (_thumbnailsSequence.PreviousThumbnail != null)
                {
                    _thumbnailsSequence.MoveToPreviousThumbnail();
                    ImageSetUp(_thumbnailsSequence.CurrentThumbnail.ImageFile);
                }
            }
        }

        private void FullScreenImage_FormClosed(object sender, FormClosedEventArgs args)
        {
            pbFullScreenImage.Image = GlobalData.InvalidImage;

            if (_imageFile != null && _imageFile != _thumbnailsSequence.CurrentThumbnail.ImageFile)
                _imageFile.DisposeImageFromDisc();

            _imageFile.DisposeFullScreenImage();
            
            _parentForm.ImageSetUp(_thumbnailsSequence.CurrentThumbnail.ImageFile);
        }

        #endregion
    }
}
